<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="订单编号">
          <el-input
            v-model="searchParam.orderNo"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item label="公司编号">
          <el-input
            v-model="searchParam.organizationCode"
            placeholder="请输入公司代码;基本信息"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item label="工厂编号">
          <el-input
            v-model="searchParam.plantCode"
            placeholder="请输入工厂代码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <div v-if="isExpanded">
          <el-form-item label="送货单号">
            <el-input
              v-model="searchParam.asnCode"
              placeholder="请输入送货单号"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>

          <el-form-item label="供应商编号">
            <el-input
              v-model="searchParam.partnerCode"
              placeholder="请输入供应商编号"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>

          <el-form-item label="送货日期">
            <el-date-picker
              v-model="searchParam.asnDateSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getasnDate"
            />
          </el-form-item>
          <el-form-item label="预计到货日期">
            <el-date-picker
              v-model="searchParam.arriveDateSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getarriveDate"
            />
          </el-form-item>
          <el-form-item label="运输方式">
            <el-input
              v-model="searchParam.modeTransport"
              placeholder="请输入运输方式"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="运单号">
            <el-input
              v-model="searchParam.trackingNumber"
              placeholder="请输入运单号"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>

          <el-form-item label="存储地点编号;要货地点">
            <el-input
              v-model="searchParam.storageLocationCode"
              placeholder="请输入存储地点编号;要货地点"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>

          <el-form-item label="承运人">
            <el-input
              v-model="searchParam.carrier"
              placeholder="请输入承运人"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="发货联系人">
            <el-input
              v-model="searchParam.shippingContactPerson"
              placeholder="请输入发货联系人"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="发货联系人电话">
            <el-input
              v-model="searchParam.shippingContactPhone"
              placeholder="请输入发货联系人电话"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="车牌号">
            <el-input
              v-model="searchParam.plateNumber"
              placeholder="请输入车牌号"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="发货状态">
            <el-select
              v-model="searchParam.shipmentStatus"
              placeholder="请选择发货状态"
              clearable
            >
              <el-option
                v-for="item in useDictionary('ShipmentStatus')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="创建时间">
            <el-date-picker
              v-model="searchParam.createTimeSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getcreateTime"
            />
          </el-form-item>

          <el-form-item label="创建人名">
            <el-input
              v-model="searchParam.createdUserName"
              placeholder="请输入创建人名"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker
              v-model="searchParam.lastUpdateTimeSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getlastUpdateTime"
            />
          </el-form-item>

          <el-form-item label="更新人名">
            <el-input
              v-model="searchParam.lastUpdatedUserName"
              placeholder="请输入更新人名"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
        </div>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
          <el-button type="primary" @click="toggleSearchArea" link>
            {{ isExpanded ? "收起" : "展开" }}
            <el-icon v-if="isExpanded" class="el-icon--right">
              <i-ep-arrowUp color="#409EFC" />
            </el-icon>
            <el-icon v-else class="el-icon--right">
              <i-ep-arrowDown color="#409EFC" />
            </el-icon>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="订单编号"
          prop="orderNo"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="公司编号"
          prop="organizationCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="公司名称"
          prop="organizationName"
          min-width="190"
          show-overflow-tooltip
        />

        <el-table-column
          label="工厂编号"
          prop="plantCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工厂名称"
          prop="plantName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="送货单号"
          prop="asnCode"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="供应商编号"
          prop="partnerCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="供应商名称"
          prop="partnerName"
          min-width="220"
          show-overflow-tooltip
        />
        <el-table-column
          label="送货日期"
          prop="asnDate"
          min-width="100"
          show-overflow-tooltip
          :formatter="formatDate"
        />
        <el-table-column
          label="预计到货日期"
          prop="arriveDate"
          min-width="120"
          show-overflow-tooltip
          :formatter="formatDate"
        />
        <el-table-column
          label="运输方式"
          prop="modeTransport"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="运单号"
          prop="trackingNumber"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="存储地点编号"
          prop="storageLocationCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="存储地点名称"
          prop="storageLocationName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="承运人"
          prop="carrier"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="发货联系人"
          prop="shippingContactPerson"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="发货联系人电话"
          prop="shippingContactPhone"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="车牌号"
          prop="plateNumber"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column label="发货状态" prop="shipmentStatus" min-width="100">
          <template #default="scope">
            {{ useDictionary("ShipmentStatus", scope.row.shipmentStatus) }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="创建人名"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="更新人名"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="handleTableDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <srmasn-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "SRMSRMASN",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/srm/srmasn/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  orderNo: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  asnCode: "",
  partnerId: "",
  partnerCode: "",
  partnerName: "",
  asnDateSearch: [],
  asnDateStart: "",
  asnDateEnd: "",
  arriveDateSearch: [],
  arriveDateStart: "",
  arriveDateEnd: "",
  modeTransport: "",
  trackingNumber: "",
  storageLocationId: "",
  storageLocationCode: "",
  storageLocationName: "",
  carrier: "",
  shippingContactPerson: "",
  shippingContactPhone: "",
  plateNumber: "",
  shipmentStatus: "",
  remarks: "",
  createTimeSearch: [],
  createTimeStart: "",
  createTimeEnd: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTimeSearch: [],
  lastUpdateTimeStart: "",
  lastUpdateTimeEnd: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
};

const deleteParam = {
  api: "/srm/srmasn/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}
const isExpanded = ref(false);
const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/srm/srmasn/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开ASN单表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改收货单";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增收货单";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开ASN单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "收货单详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}
function toggleSearchArea() {
  isExpanded.value = !isExpanded.value;
}
function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getasnDate(data: any) {
  if (data) {
    searchParam.value.asnDateStart = data[0];
    searchParam.value.asnDateEnd = data[1];
  } else {
    searchParam.value.asnDateStart = "";
    searchParam.value.asnDateEnd = "";
  }
}
function getarriveDate(data: any) {
  if (data) {
    searchParam.value.arriveDateStart = data[0];
    searchParam.value.arriveDateEnd = data[1];
  } else {
    searchParam.value.arriveDateStart = "";
    searchParam.value.arriveDateEnd = "";
  }
}
function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0];
    searchParam.value.createTimeEnd = data[1];
  } else {
    searchParam.value.createTimeStart = "";
    searchParam.value.createTimeEnd = "";
  }
}
function getlastUpdateTime(data: any) {
  if (data) {
    searchParam.value.lastUpdateTimeStart = data[0];
    searchParam.value.lastUpdateTimeEnd = data[1];
  } else {
    searchParam.value.lastUpdateTimeStart = "";
    searchParam.value.lastUpdateTimeEnd = "";
  }
}
function formatDate(row, column) {
  const date = row[column.property];
  if (!date) return "";
  return date.split(" ")[0]; // 返回日期的年月日部分，而隐藏时间部分
}
</script>
